<?php

namespace app\mec\controller;
use think\Controller;
use think\Db;
use org\Verify;
use com\Geetestlib;

class Login extends Controller
{
    //登录页面
    public function index()
    {
        return $this->fetch('/login');
    }

    //登录操作 1：数字验证码  2：极验验证
    public function doLogin()
    {
        $username = input("param.username");
        $password = input("param.password");
        if (config('verify_type') == 1) {
            $code = input("param.code");
        }
        $verify = new Verify();
        if (config('verify_type') == 1) {
            if (!$code) {
                return json(['code' => -4, 'url' => '', 'msg' => '请输入验证码']);
            }
            if (!$verify->check($code)) {
                return json(['code' => -4, 'url' => '', 'msg' => '验证码错误','status'=>100]);
            }
           return  $this->checkAdmin($username,$password);
        }elseif (config('verify_type') == 2) {

            $GtSdk = new Geetestlib(config('gee.gee_id'), config('gee.gee_key'));
            $user_id = session('user_id');
            if (session('gtserver') == 1) {
                $result = $GtSdk->success_validate(input('param.geetest_challenge'), input('param.geetest_validate'), input('param.geetest_seccode'), $user_id);
                //极验服务器状态正常的二次验证接口
                if (!$result) {
                    $this->error('请先拖动验证码到相应位置');
                }
            }else{
                if (!$GtSdk->fail_validate(input('param.geetest_challenge'), input('param.geetest_validate'), input('param.geetest_seccode'))) {
                    //极验服务器状态宕机的二次验证接口
                    $this->error('请先拖动验证码到相应位置');
                }
            }
            return  $this->checkAdmin($username,$password);
        }else{
            return  $this->checkAdmin($username,$password);
        }
    }

    //验证密码和账号
    public function checkAdmin($username,$password){

        $hasUser = Db::name('fenxiao')
            ->where('fen_username', $username)
            ->find();
        if(empty($hasUser)){
            return json(['code' => -1, 'url' => '', 'msg' => '管理员不存在',"status"=>100]);
        }

        if(md5($password) != $hasUser['fen_password']){
            return json(['code' => -2, 'url' => '', 'msg' => '密码错误',"status"=>100]);
        }

        //获取该管理员的角色信息
        session('mid', $hasUser['fen_id']);         //用户ID
        session('mname', $hasUser['fen_username']);  //用户名
        session('portrait', $hasUser['fen_image']); //用户头像
        session('last_time',time());            //角色登录时间点
        session('theme',1);     //主题

        return json(['code' => 1, 'url' => url('mec/index/index'), 'msg' => '登录成功！']);
    }

    //验证码
    public function checkVerify()
    {
        $verify = new Verify();
        $verify->imageH = 36;
        $verify->imageW = 110;
		$verify->codeSet = '023456789';
		$verify->useZh = false;//使用中文验证码
        $verify->length = 4;
        $verify->useNoise = true;//是否添加杂点
        $verify->useCurve = false;//是否画混淆曲线
        $verify->useImgBg = false;//使用背景图片
        $verify->fontSize = 16;
        return $verify->entry();
    }


    //极验验证
    public function getVerify(){
        $GtSdk = new Geetestlib(config('gee.gee_id'), config('gee.gee_key'));
        $user_id = "web";
        $status = $GtSdk->pre_process($user_id);
        session('gtserver',$status);
        session('user_id',$user_id);
        echo $GtSdk->get_response_str();
    }


    //退出操作
    public function loginOut()
    {
        session(null);
        cache('db_config_data',null);
        $this->redirect(url('mec/index/index'));
    }
}